package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.PreventionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPreventionDAO;
import cn.edu.scau.cmi.hombio.dao.UnitDAO;

import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.PreventionType;
import cn.edu.scau.cmi.hombio.domain.TaskPrevention;
import cn.edu.scau.cmi.hombio.domain.Unit;

import cn.edu.scau.cmi.hombio.service.PreventionTypeService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for PreventionType entities
 * 
 */

@Controller("PreventionTypeRestController")

public class PreventionTypeRestController {

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages PreventionType entities
	 * 
	 */
	@Autowired
	private PreventionTypeDAO preventionTypeDAO;

	/**
	 * DAO injected by Spring that manages TaskPrevention entities
	 * 
	 */
	@Autowired
	private TaskPreventionDAO taskPreventionDAO;

	/**
	 * DAO injected by Spring that manages Unit entities
	 * 
	 */
	@Autowired
	private UnitDAO unitDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for PreventionType entities
	 * 
	 */
	@Autowired
	private PreventionTypeService preventionTypeService;

	/**
	 * Delete an existing Medicine entity
	 * 
	 */
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByReportMedicineId/{medicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePreventionTypeMedicineByReportMedicineId(@PathVariable Integer preventiontype_id, @PathVariable Integer related_medicinebyreportmedicineid_id) {
		preventionTypeService.deletePreventionTypeMedicineByReportMedicineId(preventiontype_id, related_medicinebyreportmedicineid_id);
	}

	/**
	* Delete an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByUnitId/{unit_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePreventionTypeUnitByUnitId(@PathVariable Integer preventiontype_id, @PathVariable Integer related_unitbyunitid_id) {
		preventionTypeService.deletePreventionTypeUnitByUnitId(preventiontype_id, related_unitbyunitid_id);
	}

	/**
	* Create a new PreventionType entity
	* 
	*/
	@RequestMapping(value = "/PreventionType", method = RequestMethod.POST)
	@ResponseBody
	public PreventionType newPreventionType(@RequestBody PreventionType preventiontype) {
		preventionTypeService.savePreventionType(preventiontype);
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype.getId());
	}

	/**
	* Delete an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePreventionType(@PathVariable Integer preventiontype_id) {
		PreventionType preventiontype = preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype_id);
		preventionTypeService.deletePreventionType(preventiontype);
	}

	/**
	* Save an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByReportMedicineId", method = RequestMethod.PUT)
	@ResponseBody
	public Medicine savePreventionTypeMedicineByReportMedicineId(@PathVariable Integer preventiontype_id, @RequestBody Medicine medicinebyreportmedicineid) {
		preventionTypeService.savePreventionTypeMedicineByReportMedicineId(preventiontype_id, medicinebyreportmedicineid);
		return medicineDAO.findMedicineByPrimaryKey(medicinebyreportmedicineid.getId());
	}

	/**
	* Save an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/PreventionType", method = RequestMethod.PUT)
	@ResponseBody
	public PreventionType savePreventionType(@RequestBody PreventionType preventiontype) {
		preventionTypeService.savePreventionType(preventiontype);
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype.getId());
	}

	/**
	* Save an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByUnitId", method = RequestMethod.PUT)
	@ResponseBody
	public Unit savePreventionTypeUnitByUnitId(@PathVariable Integer preventiontype_id, @RequestBody Unit unitbyunitid) {
		preventionTypeService.savePreventionTypeUnitByUnitId(preventiontype_id, unitbyunitid);
		return unitDAO.findUnitByPrimaryKey(unitbyunitid.getId());
	}

	/**
	* Get Unit entity by PreventionType
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByUnitId", method = RequestMethod.GET)
	@ResponseBody
	public Unit getPreventionTypeUnitByUnitId(@PathVariable Integer preventiontype_id) {
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype_id).getUnitByUnitId();
	}

	/**
	* View an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByUnitId/{unit_id}", method = RequestMethod.GET)
	@ResponseBody
	public Unit loadPreventionTypeUnitByUnitId(@PathVariable Integer preventiontype_id, @PathVariable Integer related_unitbyunitid_id) {
		Unit unit = unitDAO.findUnitByPrimaryKey(related_unitbyunitid_id, -1, -1);

		return unit;
	}

	/**
	* Create a new Unit entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByReportUnitId", method = RequestMethod.POST)
	@ResponseBody
	public Unit newPreventionTypeUnitByReportUnitId(@PathVariable Integer preventiontype_id, @RequestBody Unit unit) {
		preventionTypeService.savePreventionTypeUnitByReportUnitId(preventiontype_id, unit);
		return unitDAO.findUnitByPrimaryKey(unit.getId());
	}

	/**
	* Save an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByReportUnitId", method = RequestMethod.PUT)
	@ResponseBody
	public Unit savePreventionTypeUnitByReportUnitId(@PathVariable Integer preventiontype_id, @RequestBody Unit unitbyreportunitid) {
		preventionTypeService.savePreventionTypeUnitByReportUnitId(preventiontype_id, unitbyreportunitid);
		return unitDAO.findUnitByPrimaryKey(unitbyreportunitid.getId());
	}

	/**
	* Create a new TaskPrevention entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/taskPreventions", method = RequestMethod.POST)
	@ResponseBody
	public TaskPrevention newPreventionTypeTaskPreventions(@PathVariable Integer preventiontype_id, @RequestBody TaskPrevention taskprevention) {
		preventionTypeService.savePreventionTypeTaskPreventions(preventiontype_id, taskprevention);
		return taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention.getId());
	}

	/**
	* View an existing TaskPrevention entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/taskPreventions/{taskprevention_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskPrevention loadPreventionTypeTaskPreventions(@PathVariable Integer preventiontype_id, @PathVariable Integer related_taskpreventions_id) {
		TaskPrevention taskprevention = taskPreventionDAO.findTaskPreventionByPrimaryKey(related_taskpreventions_id, -1, -1);

		return taskprevention;
	}

	/**
	* View an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByMedicineId/{medicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public Medicine loadPreventionTypeMedicineByMedicineId(@PathVariable Integer preventiontype_id, @PathVariable Integer related_medicinebymedicineid_id) {
		Medicine medicine = medicineDAO.findMedicineByPrimaryKey(related_medicinebymedicineid_id, -1, -1);

		return medicine;
	}

	/**
	* Select an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}", method = RequestMethod.GET)
	@ResponseBody
	public PreventionType loadPreventionType(@PathVariable Integer preventiontype_id) {
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype_id);
	}

	/**
	* Delete an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByReportUnitId/{unit_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePreventionTypeUnitByReportUnitId(@PathVariable Integer preventiontype_id, @PathVariable Integer related_unitbyreportunitid_id) {
		preventionTypeService.deletePreventionTypeUnitByReportUnitId(preventiontype_id, related_unitbyreportunitid_id);
	}

	/**
	* Show all PreventionType entities
	* 
	*/
	@RequestMapping(value = "/PreventionType", method = RequestMethod.GET)
	@ResponseBody
	public List<PreventionType> listPreventionTypes() {
		return new java.util.ArrayList<PreventionType>(preventionTypeService.loadPreventionTypes());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Get Unit entity by PreventionType
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByReportUnitId", method = RequestMethod.GET)
	@ResponseBody
	public Unit getPreventionTypeUnitByReportUnitId(@PathVariable Integer preventiontype_id) {
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype_id).getUnitByReportUnitId();
	}

	/**
	* Save an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByMedicineId", method = RequestMethod.PUT)
	@ResponseBody
	public Medicine savePreventionTypeMedicineByMedicineId(@PathVariable Integer preventiontype_id, @RequestBody Medicine medicinebymedicineid) {
		preventionTypeService.savePreventionTypeMedicineByMedicineId(preventiontype_id, medicinebymedicineid);
		return medicineDAO.findMedicineByPrimaryKey(medicinebymedicineid.getId());
	}

	/**
	* Show all TaskPrevention entities by PreventionType
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/taskPreventions", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskPrevention> getPreventionTypeTaskPreventions(@PathVariable Integer preventiontype_id) {
		return new java.util.ArrayList<TaskPrevention>(preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype_id).getTaskPreventions());
	}

	/**
	* Get Medicine entity by PreventionType
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByReportMedicineId", method = RequestMethod.GET)
	@ResponseBody
	public Medicine getPreventionTypeMedicineByReportMedicineId(@PathVariable Integer preventiontype_id) {
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype_id).getMedicineByReportMedicineId();
	}

	/**
	* Delete an existing TaskPrevention entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/taskPreventions/{taskprevention_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePreventionTypeTaskPreventions(@PathVariable Integer preventiontype_id, @PathVariable Integer related_taskpreventions_id) {
		preventionTypeService.deletePreventionTypeTaskPreventions(preventiontype_id, related_taskpreventions_id);
	}

	/**
	* View an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByReportUnitId/{unit_id}", method = RequestMethod.GET)
	@ResponseBody
	public Unit loadPreventionTypeUnitByReportUnitId(@PathVariable Integer preventiontype_id, @PathVariable Integer related_unitbyreportunitid_id) {
		Unit unit = unitDAO.findUnitByPrimaryKey(related_unitbyreportunitid_id, -1, -1);

		return unit;
	}

	/**
	* Save an existing TaskPrevention entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/taskPreventions", method = RequestMethod.PUT)
	@ResponseBody
	public TaskPrevention savePreventionTypeTaskPreventions(@PathVariable Integer preventiontype_id, @RequestBody TaskPrevention taskpreventions) {
		preventionTypeService.savePreventionTypeTaskPreventions(preventiontype_id, taskpreventions);
		return taskPreventionDAO.findTaskPreventionByPrimaryKey(taskpreventions.getId());
	}

	/**
	* Create a new Medicine entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByReportMedicineId", method = RequestMethod.POST)
	@ResponseBody
	public Medicine newPreventionTypeMedicineByReportMedicineId(@PathVariable Integer preventiontype_id, @RequestBody Medicine medicine) {
		preventionTypeService.savePreventionTypeMedicineByReportMedicineId(preventiontype_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* Create a new Medicine entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByMedicineId", method = RequestMethod.POST)
	@ResponseBody
	public Medicine newPreventionTypeMedicineByMedicineId(@PathVariable Integer preventiontype_id, @RequestBody Medicine medicine) {
		preventionTypeService.savePreventionTypeMedicineByMedicineId(preventiontype_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* Create a new Unit entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/unitByUnitId", method = RequestMethod.POST)
	@ResponseBody
	public Unit newPreventionTypeUnitByUnitId(@PathVariable Integer preventiontype_id, @RequestBody Unit unit) {
		preventionTypeService.savePreventionTypeUnitByUnitId(preventiontype_id, unit);
		return unitDAO.findUnitByPrimaryKey(unit.getId());
	}

	/**
	* Delete an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByMedicineId/{medicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePreventionTypeMedicineByMedicineId(@PathVariable Integer preventiontype_id, @PathVariable Integer related_medicinebymedicineid_id) {
		preventionTypeService.deletePreventionTypeMedicineByMedicineId(preventiontype_id, related_medicinebymedicineid_id);
	}

	/**
	* Get Medicine entity by PreventionType
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByMedicineId", method = RequestMethod.GET)
	@ResponseBody
	public Medicine getPreventionTypeMedicineByMedicineId(@PathVariable Integer preventiontype_id) {
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype_id).getMedicineByMedicineId();
	}

	/**
	* View an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/PreventionType/{preventiontype_id}/medicineByReportMedicineId/{medicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public Medicine loadPreventionTypeMedicineByReportMedicineId(@PathVariable Integer preventiontype_id, @PathVariable Integer related_medicinebyreportmedicineid_id) {
		Medicine medicine = medicineDAO.findMedicineByPrimaryKey(related_medicinebyreportmedicineid_id, -1, -1);

		return medicine;
	}
}